package com.zjkj.mp.controller.userManage;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zjkj.mp.service.user.UserService;
import com.zjkj.mp.utils.security.MD5Encrypt;
import com.zjkj.mp.vo.user.MpUser;
import com.zjkj.mp.vo.user.USER_ZT;
import com.zjkj.mp.vo.user.UserRoles;

/**
 * 
 * 新建用户控制器
 *
 * @author 陈晨
 * @history
 * <TABLE id="HistoryTable" border="1">
 * 	<TR><TD>时间</TD><TD>描述</TD><TD>作者</TD></TR>
 *	<TR><TD>2013-6-9</TD><TD>创建初始版本</TD><TD>陈晨</TD></TR>
 * </TABLE>
 */
@Controller
public class NewUserController {
	private Log logger = LogFactory.getLog(getClass());
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	@Resource(name = "userServiceImpl")
	private UserService userService;

	 
	/**
	 * 跳转至新建用户页面
	 * @return
	 */
	@RequestMapping("/newUser.do")
	public String newUser() {
		return "user/newUser";
	} 
	
	/**
	 * 添加一个用户
	 */
	@RequestMapping("/doNewUser.do")
	public String doAddUser(HttpServletRequest req){
		//得到新建用户信息
		String realName = req.getParameter("realName");
		String mobile = req.getParameter("mobile");
		String phone = req.getParameter("phone");
		String password = req.getParameter("password");
		String birthday = req.getParameter("birthday");
		String unit = req.getParameter("unit");
		String address = req.getParameter("address");
		String sex = req.getParameter("sex");
		String note = req.getParameter("note");
		String email = req.getParameter("email");
		String isAudit = req.getParameter("isAudit");
		 

		//组装对象
		MpUser user  = new MpUser();
		user.setRealName(realName);
		user.setMobile(mobile);
		user.setPhone(phone);
		user.setPassword(MD5Encrypt.encode(password));
		user.setBirthDay(birthday);
		user.setUnit(unit);
		user.setAddress(address);
		user.setSex(sex);
		user.setNote(note);
		user.setEmail(email);
		user.setUserState(USER_ZT.SYZ);
		user.setRoleID(UserRoles.COMMON);
		user.setCreateDate(sdf.format(new Date()));
		user.setAudit(isAudit.equals("true")?true:false);
		
		//校验合法性
		String errorMsg = validateUserInfo(user);
		
		//保存用户信息到数据库中
		try{
			if(errorMsg == null){
				this.userService.addUser(user);
			}			
		}catch(Exception e){
			logger.error("添加用户失败",e);
			errorMsg = "系统内部错误，添加用户失败！";
		}		
		
		req.setAttribute("errorMsg", errorMsg);
		req.setAttribute("user", user);
	 	
		return "user/userDetail";		
	}

	/**
	 * 校验用户信息的合法性
	 * @return 如果返回null表示校验通过，否则返回错误信息。
	 */
	private String validateUserInfo(MpUser user) {
		//校验邮箱是否已经注册
		MpUser target = this.userService.findByEmail(user.getEmail());
		if(target != null){
			return "此邮箱已经被注册！";
		}
		return null;
	}
}
